package com.example.rnademo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;
    private Boolean Debug = true;
    private TextView des;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        des = findViewById(R.id.activity_main_des);
        initWebView();
    }

    private void initWebView() {
        mWebView = findViewById(R.id.activity_main_wv);
        //mWebView.loadUrl("https://blog.csdn.net/u010886535/article/details/109992969");
        mWebView.loadUrl("file:///android_asset/web/index.html");
        // 启用javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        /*
         * 添加javascriptInterface
         * 第一个参数：这里需要一个与js映射的java对象
         * 第二个参数：该java对象被映射为js对象后在js里面的对象名，在js中要调用该对象的方法就是通过这个来调用
         */
        mWebView.addJavascriptInterface(new NativeToJsBridge(), "NativeToJsBridge");
    }

    public void nativeToJs1(View view) {
        // 无参数调用
        mWebView.loadUrl("javascript:loadUrl()");
    }

    public void nativeToJs2(View view) {
        // 传递参数调用
        mWebView.loadUrl("javascript:loadUrlArgs('" + "我是loadUrl" + "')");

    }

    public void nativeToJs3(View view) {
        mWebView.evaluateJavascript("javascript:evaluateJavascript()", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Log.d("MainActivity", "nativeToJs3:" + value + " " + Thread.currentThread() + " " + Log.getStackTraceString(new Throwable()));
                des.append("onReceiveValue:" + value + "\n");
            }
        });
    }

    public void nativeToJs4(View view) {
        mWebView.evaluateJavascript("javascript:evaluateJavascriptArgs('" + "我是evaluateJavascript" + "');", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Log.d("MainActivity", "nativeToJs4:" + value + " " + Thread.currentThread() + " " + Log.getStackTraceString(new Throwable()));
                des.append("onReceiveValue:" + value + "\n");
            }
        });
        //2021-06-07 08:03:25.431 29133-29133/com.example.rnademo1 D/MainActivity: nativeToJs4:null Thread[main,5,main] java.lang.Throwable
        //        at com.example.rnademo1.MainActivity$2.onReceiveValue(MainActivity.java:66)
        //        at com.example.rnademo1.MainActivity$2.onReceiveValue(MainActivity.java:63)
        //        at hm.a(Unknown Source:2)
        //        at org.chromium.android_webview.AwContents.a(PG:1025)
        //        at zp.run(Unknown Source:4)
        //        at android.os.Handler.handleCallback(Handler.java:888)
        //        at android.os.Handler.dispatchMessage(Handler.java:100)
        //        at android.os.Looper.loop(Looper.java:213)
        //        at android.app.ActivityThread.main(ActivityThread.java:8169)
        //        at java.lang.reflect.Method.invoke(Native Method)
        //        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        //        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
    }

    @SuppressWarnings("unused")
    private final class NativeToJsBridge {
        /**
         * 注意这里的@JavascriptInterface注解， target是4.2以上都需要添加这个注解，否则无法调用
         */
        @JavascriptInterface
        public void call() {
            if (Debug) {
                Log.d("MainActivity", "call " + Thread.currentThread() + " " + Log.getStackTraceString(new Throwable()));
            } else {
                Log.d("MainActivity", "call " + Thread.currentThread());
            }
            runOnUiThread(() -> des.append("js call native -> call\n"));
        }
        //2021-06-07 08:03:27.450 29133-29315/com.example.rnademo1 D/MainActivity: call Thread[JavaBridge,7,main] java.lang.Throwable
        //        at com.example.rnademo1.MainActivity$NativeToJsBridge.call(MainActivity.java:79)
        //        at android.os.MessageQueue.nativePollOnce(Native Method)
        //        at android.os.MessageQueue.next(MessageQueue.java:363)
        //        at android.os.Looper.loop(Looper.java:173)
        //        at android.os.HandlerThread.run(HandlerThread.java:67)


        @JavascriptInterface
        public void callWithArgs(String data) {
            if (Debug) {
                Log.d("MainActivity", "callWithArgs:" + data + " " + Thread.currentThread() + " " + Log.getStackTraceString(new Throwable()));
            } else {
                Log.d("MainActivity", "callWithArgs:" + data + " " + Thread.currentThread());
            }
            runOnUiThread(() -> des.append("js call native -> callTwoWithArgs:" + data + "\n"));
        }
        //2021-06-07 08:03:28.263 29133-29315/com.example.rnademo1 D/MainActivity: callWithArgs:我是来自js的参数 Thread[JavaBridge,7,main] java.lang.Throwable
        //        at com.example.rnademo1.MainActivity$NativeToJsBridge.callWithArgs(MainActivity.java:96)
        //        at android.os.MessageQueue.nativePollOnce(Native Method)
        //        at android.os.MessageQueue.next(MessageQueue.java:363)
        //        at android.os.Looper.loop(Looper.java:173)
        //        at android.os.HandlerThread.run(HandlerThread.java:67)
    }

}
